import { ActivatedRoute } from '@angular/router';
import { ReletApply, ReletApplyList } from './../../domains/reletApplyList.domain';
import { NzMessageService } from 'ng-zorro-antd/message';
import { LocalStorage } from './../../service/localStorage.service';
import { Component, OnInit } from '@angular/core';
import { ReletApplyServiceSvr } from 'src/app/service/reletApply.service';

@Component({
  selector: 'app-rent-management-renewal',
  templateUrl: './rent-management-renewal.component.html',
  styleUrls: ['./rent-management-renewal.component.less']
})
export class RentManagementRenewalComponent implements OnInit {

  constructor(
    private reletApplyServiceSvr: ReletApplyServiceSvr,
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute
  ){ }

  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  houseId: string = ''
  customerName: string = JSON.parse(localStorage.getItem('customerNickName')!)
  reletList: any
  reletListFilter: ReletApplyList = new ReletApplyList()
  IsApply: boolean = false
  addModal: boolean = false
  applyReason: string = ''

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.houseId = JSON.parse(queryParams['houseData']).Id
    })
    this.getReletApplyList()

  }


  // 获取续租列表
  async getReletApplyList() {
    let result  = await this.reletApplyServiceSvr.listReletApply(this.token, this.customerId, 0, '', '', '', 2, 1, -1 )
    console.log(result)
    this.reletList = result._Items
    this.reletListFilter = this.reletList
    this.IsApplyRelet()
  }

  // 过滤通过续租列表
  filterReletApplyList_state(state: number) {
    this.reletListFilter = this.reletList.filter((item: ReletApply)=>{
      if(item.State===state) return item
      return;
    })
  }

  // 过滤撤销续租列表
  filterReletApplyList_isBackout(isBackout: boolean) {
    this.reletListFilter = this.reletList.filter((item: ReletApply)=>{
      if(item.IsBackout===isBackout) return item
      return;
    })
  }

  // 格式化时间
  formatTime(time: string | number | Date) {
    time = new Date(time)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    return year+'-'+month+'-'+date
  }

  // 撤销申请
  async cancelApply(id: string) {
    let res = await this.reletApplyServiceSvr.deleteReletApply(this.token, id)
    if(res===1) {
      this.message.success('撤销成功')
      console.log(res)
      this.getReletApplyList()
    } else {
      this.message.error('撤销失败')
    }
  }

  // 是否可以申请
  async IsApplyRelet() {
    let res = await this.reletApplyServiceSvr.checkIsRedo(this.token, this.customerId, this.houseId)
    if(res===1) {
      this.IsApply = false
    }
    if(res===-1) {
      this.IsApply = true
    }
  }

  // 续租申请
  addReletApply() {
    this.addModal = true
  }

  handleCancelReletApply() {
    this.addModal = false
    this.applyReason = ''
  }

  async handleOkRelet() {
    console.log(this.applyReason)
    let res = await this.reletApplyServiceSvr.saveReletApply(this.token, this.customerId, this.houseId, this.applyReason)
    if(res===1) {
      this.message.success('续租申请成功')
    } else {
      this.message.error('续租申请失败')
    }
    this.addModal = false
    this.applyReason = ''
    this.getReletApplyList()
  }

}
